package com.zhaopin.qa.jmeter.plugins.multienvuser.threads;

import java.awt.BorderLayout;

import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.threads.AbstractThreadGroup;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.AbstractThreadGroupGui;

import com.zhaopin.qa.jmeter.plugins.multienvuser.controller.MultiEnvUserController;
import com.zhaopin.qa.jmeter.plugins.multienvuser.controller.MultiEnvUserControllerPanel;

public class MultiEnvUserThreadGroupGui extends AbstractThreadGroupGui {

	private static final long serialVersionUID = -394914559839491874L;

	private MultiEnvUserControllerPanel controllerPanel;

	public MultiEnvUserThreadGroupGui() {
		initGui();
	}

	public MultiEnvUserControllerPanel createControllerPanel() {
		controllerPanel = new MultiEnvUserControllerPanel(false);
		MultiEnvUserController controller = (MultiEnvUserController) controllerPanel.createTestElement();
		controllerPanel.configure(controller);
		return controllerPanel;
	}

	private void initGui() {
		setLayout(new BorderLayout());
		setBorder(makeBorder());
		add(makeTitlePanel(), BorderLayout.NORTH);

		add(createControllerPanel(), BorderLayout.CENTER);
	}

	@Override
	public String getStaticLabel() {
		return "Multi-EnvUser Thread Group";
	}

	@Override
	public String getLabelResource() {
		return "多环境多用户线程组";
	}

	@Override
	public TestElement createTestElement() {
		ThreadGroup tg = new ThreadGroup();
		modifyTestElement(tg);
		return tg;
	}

	@Override
	public void modifyTestElement(TestElement tg) {
		super.configureTestElement(tg);
		if (tg instanceof AbstractThreadGroup) {
			MultiEnvUserController multiEnvUserController = (MultiEnvUserController) controllerPanel
					.createTestElement();
			((AbstractThreadGroup) tg).setSamplerController(multiEnvUserController);
			int size = multiEnvUserController.getSelectedUser().size();
			((AbstractThreadGroup) tg).setNumThreads(size);
		}
	}

	@Override
	public void configure(TestElement tg) {
		super.configure(tg);
		if (tg instanceof AbstractThreadGroup) {
			controllerPanel.configure(((AbstractThreadGroup) tg).getSamplerController());
		}
	}

	@Override
	public void clearGui() {
		super.clearGui();
		controllerPanel.clearGui();
	}
}
